package com.dgh.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dgh.school.bean.Department;
import com.dgh.school.common.R;
import com.dgh.school.mapper.DepartmentMapper;
import com.dgh.school.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public R selectByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        // 查询全部数据
        List<Department> departments = departmentMapper.selectAll();
        // 借助分页助手，获得分页信息
        PageInfo<Department> pageInfo = new PageInfo<>(departments);

        return R.ok().setDate("pageInfo", pageInfo);
    }

    @Override
    public void deleteById(int id) throws Exception {
        departmentMapper.deleteById(id);
    }

    @Override
    public void insertData(Department de) throws Exception {
        departmentMapper.insert(de);
    }

    @Override
    public void updateData(Department de) throws Exception {
        departmentMapper.update(de);
    }

    @Override
    public R selectAlldepartments() throws Exception {
        List<Department> departments =  departmentMapper.selectAll();
        return R.ok().setDate("departments",departments);
    }
}